package com.jianjian.gulimall.product.service.impl;

import com.jianjian.gulimall.product.entity.CategoryBrandRelationEntity;
import com.jianjian.gulimall.product.service.CategoryBrandRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jianjian.common.utils.PageUtils;
import com.jianjian.common.utils.Query;

import com.jianjian.gulimall.product.dao.CategoryDao;
import com.jianjian.gulimall.product.entity.CategoryEntity;
import com.jianjian.gulimall.product.service.CategoryService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {


    @Resource
    private CategoryBrandRelationService categoryBrandRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntity> listWithTree() {
        //查询所有数据
        List<CategoryEntity> categoryList = baseMapper.selectList(null);

        //找出所有的一级菜单
        List<CategoryEntity> level1Menu = categoryList.stream().filter(categoryEntity ->
            //不知道为什么这里用不了equals
            categoryEntity.getParentCid() == 0
        ).map((categoryEntity) -> {
            //查询子菜单
            categoryEntity.setChildren(getChildrens(categoryEntity,categoryList));
            return categoryEntity;
        }).sorted((menu1,menu2)->{
            //排序
            return (menu1.getSort() == null ? 0 : menu1.getSort()) - (menu2.getSort() == null ? 0 : menu2.getSort());
        }).collect(Collectors.toList());

        return level1Menu;
    }

    /**
     *
     * @param asList id集合
     */
    @Override
    public void removeMenuByIds(List<Long> asList) {
         //TODO   1.检查当前删除的菜单，是否被别的地方引用

        //逻辑删除
        baseMapper.deleteBatchIds(asList);

    }

    /**
     * 找到catelogId的完整路径
     * [父/子/子]
     * @param catelogId
     * @return
     */
    @Override
    public Long[] findCatelogPath(Long catelogId) {
    List<Long> paths = new ArrayList<>();
        List<Long> path = findByParentPath(catelogId, paths);
        Collections.reverse(path);
        return path.toArray(new Long[path.size()]);
    }

    @Transactional
    @Override
    public void updateCascade(CategoryEntity category) {
        this.updateById(category);

        categoryBrandRelationService.updateCategory(category.getCatId(),category.getName());

    }

    /**
     * 递归查找父id
     * @param catelogId
     * @param paths
     * @return
     */
    private List<Long> findByParentPath(Long catelogId,List<Long> paths){
       paths.add(catelogId);
        CategoryEntity byId = this.getById(catelogId);
        if (byId.getParentCid() != 0 ){
            findByParentPath(byId.getParentCid(),paths);
        }
        return paths;
    }


    /**
     * 递归查询所有子菜单
     * @param root  一级菜单
     * @param all   所有菜单数据
     * @return
     */
    private List<CategoryEntity> getChildrens(CategoryEntity root , List<CategoryEntity> all ){
        List<CategoryEntity> children = all.stream().filter(categoryEntity ->
                //查询二级菜单
                categoryEntity.getParentCid().equals(root.getCatId())
                ).map(categoryEntity -> {
             //递归查询二级菜单的子菜单
            categoryEntity.setChildren(getChildrens(categoryEntity, all));
            return categoryEntity;
        }).sorted((menu1,menu2)->{
            //排序
            return (menu1.getSort() == null ? 0 : menu1.getSort()) - (menu2.getSort() == null ? 0 : menu2.getSort());
        }).collect(Collectors.toList());

        return children;
    }

}